﻿using JClientAPI.Contacts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace JClient.Contacts {
	public class RosterContact : INotifyPropertyChanged {
		#region Vars
		private agsXMPP.Jid jid;
		public agsXMPP.Jid Jid {
			get {
				return this.jid;
			}
			set {
				this.jid = value;
				this.NotifyPropertyChanged("Jid");
			}
		}
		//Presence
		public StatusType StatusType {
			get {
				if (this.presenceType == agsXMPP.protocol.client.PresenceType.available) {
					switch (this.showType) {
						case agsXMPP.protocol.client.ShowType.away:
							return StatusType.away;
						case agsXMPP.protocol.client.ShowType.chat:
							return StatusType.chat;
						case agsXMPP.protocol.client.ShowType.dnd:
							return StatusType.dnd;
						case agsXMPP.protocol.client.ShowType.xa:
							return StatusType.xa;
						default:
							return StatusType.available;
					}
				} else if (this.presenceType == agsXMPP.protocol.client.PresenceType.invisible) {
					return StatusType.invisible;
				} else
					return StatusType.unavailable;
			}
		}
		private agsXMPP.protocol.client.ShowType showType;
		public agsXMPP.protocol.client.ShowType ShowType {
			get {
				return this.showType;
			}
			set {
				this.showType = value;
				this.NotifyPropertyChanged("ShowType");
				this.NotifyPropertyChanged("StatusType");
			}
		}
		private agsXMPP.protocol.client.PresenceType presenceType;
		public agsXMPP.protocol.client.PresenceType PresenceType {
			get {
				return this.presenceType;
			}
			set {
				this.presenceType = value;
				this.NotifyPropertyChanged("PresenceType");
				this.NotifyPropertyChanged("StatusType");
			}
		}
		private string status;
		public string Status {
			get {
				return this.status;
			}
			set {
				this.status = value;
				this.NotifyPropertyChanged("Status");
			}
		}
		//Roster
		private string nickname;
		public string Nickname {
			get {
				return this.nickname;
			}
			set {
				this.nickname = value;
				this.NotifyPropertyChanged("Nickname");
			}
		}
		private agsXMPP.Xml.Dom.ElementList groups;
		public agsXMPP.Xml.Dom.ElementList Groups {
			get {
				return this.groups;
			}
			set {
				this.groups = value;
				this.NotifyPropertyChanged("Groups");
			}
		}
		private agsXMPP.protocol.iq.roster.SubscriptionType subscriptionType;
		public agsXMPP.protocol.iq.roster.SubscriptionType SubscriptionType {
			get {
				return this.subscriptionType;
			}
			set {
				this.subscriptionType = value;
				this.NotifyPropertyChanged("SubscriptionType");
			}
		}
		//Vcard
		private agsXMPP.protocol.iq.vcard.Vcard vcard;
		public agsXMPP.protocol.iq.vcard.Vcard Vcard {
			get {
				return this.vcard;
			}
			set {
				this.vcard = value;
				this.NotifyPropertyChanged("Vcard");
			}
		}
		//Outher
		private bool blocked;
		public bool Blocked {
			get {
				return this.blocked;
			}
			set {
				this.blocked = value;
				this.NotifyPropertyChanged("Blocked");
			}
		}
		#endregion

		public RosterContact( ) {
			this.Jid = new agsXMPP.Jid("");
			//Presence
			this.showType = agsXMPP.protocol.client.ShowType.NONE;
			this.presenceType = agsXMPP.protocol.client.PresenceType.unavailable;
			this.status = "";
			//Roster
			this.nickname = "";
			this.groups = new agsXMPP.Xml.Dom.ElementList( );
			this.subscriptionType = agsXMPP.protocol.iq.roster.SubscriptionType.both;
			//Vcard
			this.vcard = new agsXMPP.protocol.iq.vcard.Vcard( ) {
				Birthday = new DateTime( ),
				Description = "",
				Fullname = "",
				JabberId = new agsXMPP.Jid(""),
				Name = new agsXMPP.protocol.iq.vcard.Name( ) {
					Given = "",
					Middle = "",
					Family = ""
				},
				Nickname = "",
				Organization = new agsXMPP.protocol.iq.vcard.Organization( ) {
					Name = ""
				},
				Photo = new agsXMPP.protocol.iq.vcard.Photo( ),
				Role = "",
				Title = "",
				Url = ""
			};
			//Outher
			this.blocked = false;
		}
		public RosterContact(agsXMPP.protocol.iq.roster.RosterItem rosterItem) {
			this.Jid = rosterItem.Jid;
			this.Nickname = rosterItem.Name;
			this.SubscriptionType = rosterItem.Subscription;
			this.Groups = rosterItem.GetGroups( );
			this.Vcard = new agsXMPP.protocol.iq.vcard.Vcard( ) {
				JabberId = rosterItem.Jid
			};
		}

		public void updateByPresence(agsXMPP.protocol.client.Presence presence) {
			this.Jid = presence.From;
			this.ShowType = presence.Show;
			this.PresenceType = presence.Type;
			this.Status = presence.Status;
		}

		public void updateByRoster(agsXMPP.protocol.iq.roster.RosterItem rosterItem) {
			if (this.Nickname != rosterItem.Name)
				this.Nickname = rosterItem.Name;
			if (this.SubscriptionType != rosterItem.Subscription)
				this.SubscriptionType = rosterItem.Subscription;
			if (this.Groups != rosterItem.GetGroups( ))
				this.Groups = rosterItem.GetGroups( );

		}

		public event PropertyChangedEventHandler PropertyChanged;
		private void NotifyPropertyChanged(String info) {
			if (PropertyChanged != null) {
				PropertyChanged(this, new PropertyChangedEventArgs(info));
			}
		}
	}
}